//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：UIFrameStoreExpand
// *描述：缓存管理器脚本
// *?2021 MYJL . All rights reserved.
//* ======================================================================

using System;
using System.Collections.Generic;
using UnityEngine;

namespace HDMCode.UIFrame
{
    public class UIFrameStoreExpand : UIFrameStore
    {
        public UIFrameStoreExpand()
        {
            base.InitStoreDate();
        }

        /// <summary>
        /// 尝试根据UI类型写入缓存器
        /// </summary>
        /// <param name="UI">UI对象</param>
        /// <param name="succeedBack">操作的回调</param>
        /// <returns>返回结果</returns>
        public bool TryAddToStore(UIDisplay UI, Action succeedBack = null)
        {
            bool result = false;

            if (ReadStore(UI.wrapperType, out var readStore))
            {
                result = UI.showType switch
                {
                    ShowType.Focussing => AddFocussing(),
                    ShowType.Standard => AddStandard(),
                    ShowType.Queue => AddQueue(),
                    _ => false,
                };

                bool AddFocussing()
                {
                    succeedBack?.Invoke();
                    UIFrame.Log($"尝试将【焦点类型】UI添加至存储管理器成功", UI, LogSystem.Store);
                    return true;
                }

                bool AddStandard()
                {
                    try
                    {
                        readStore.standard.Add(UI.name, UI);
                        allUIDict.Add(UI.name, UI);
                        succeedBack?.Invoke();
                        UIFrame.Log($"尝试将【标准类型】UI添加至存储管理器成功", UI, LogSystem.Store);
                        return true;
                    }
                    catch (Exception error)
                    {
                        UIFrame.LogError($"尝试将【标准类型】UI添加至存储管理器失败! {error}", UI, LogSystem.Store);
                        return false;
                    }
                }

                bool AddQueue()
                {
                    try
                    {
                        readStore.queue.Push(UI);
                        allUIDict.Add(UI.name, UI);
                        succeedBack?.Invoke();
                        UIFrame.Log($"尝试将【队列类型】UI添加至存储管理器成功", UI, LogSystem.Store);
                        return true;
                    }
                    catch (Exception error)
                    {
                        UIFrame.LogError($"尝试将【队列类型】UI添加至存储管理器失败! {error}", UI, LogSystem.Store);
                        return false;
                    }
                }
            }
            return result;
        }


        /// <summary>
        /// 尝试从缓存器中移除隐藏UI
        /// </summary>
        /// <param name="UI">UI对象</param>
        /// <param name="succeedBack">操作成功的回调</param>
        /// <returns></returns>
        public bool TryRemoveToStore(UIDisplay UI, Action<UIDisplay> succeedBack = null)
        {
            bool result = false;
            if (ReadStore(UI.wrapperType, out var readStore))
            {
                result = UI.showType switch
                {
                    ShowType.Focussing => RemoveFocussing(),
                    ShowType.Standard => RemoveStandard(),
                    ShowType.Queue => RemoveQueue(),
                    _ => false,
                };

                bool RemoveFocussing()
                {
                    succeedBack?.Invoke(UI);
                    UIFrame.Log($"尝试将【焦点类型】从存储管理器中移除成功！", UI, LogSystem.Store);
                    return true;
                }

                bool RemoveStandard()
                {
                    try
                    {
                        readStore.standard.Remove(UI.name);
                        succeedBack?.Invoke(UI);
                        UIFrame.Log($"尝试将【标准类型】从存储管理器中移除成功！", UI, LogSystem.Store);
                        return true;
                    }
                    catch (Exception error)
                    {
                        UIFrame.LogError($"尝试将【标准类型】从存储管理器中移除失败! {error}", UI, LogSystem.Store);
                        return false;
                    }
                }


                bool RemoveQueue()
                {
                    try
                    {

                        if (readStore.queue.Contains(UI))
                        {
                            InsideRemove();
                            void InsideRemove()
                            {
                                UIDisplay insideUI = readStore.queue.Pop();
                                allUIDict.Remove(insideUI.name);
                                succeedBack?.Invoke(insideUI);
                                if (insideUI.name != UI.name)
                                {
                                    InsideRemove();
                                }
                            }
                            UIFrame.Log($"尝试将【队列类型】从存储管理器中移除成功！", UI, LogSystem.Store);
                            return true;
                        }
                        else
                        {
                            UIFrame.LogError($"尝试将【队列类型】从存储管理器中移除，但是并未在队列中找到！", UI, LogSystem.Store);
                            return false;
                        }
                    }
                    catch (Exception error)
                    {
                        UIFrame.LogError($"尝试将【队列类型】从存储管理器中移除失败! {error}", UI, LogSystem.Store);
                        return false;
                    }
                }

            }
            return result;
        }

        /// <summary>
        /// 尝试从缓存器中彻底移除
        /// </summary>
        /// <param name="UI">UI对象</param>
        /// <param name="succeedBack">删除成功回调</param>
        /// <returns></returns>
        public bool TryDestroyToStore(UIDisplay UI, Action<UIDisplay> succeedBack = null)
        {
            if (TryRemoveToStore(UI, succeedBack) && allUIDict.Remove(UI.name))
            {
                UIFrame.Log($"尝试从缓存器中完全移除数据成功!", UI, LogSystem.Store);
                return true;
            }
            else
            {
                UIFrame.LogError($"尝试从缓存器中完全移除数据失败!", UI, LogSystem.Store);
                return false;
            }
        }


        /// <summary>
        /// 移除指定缓存类型中的所有数据
        /// </summary>
        /// <returns></returns>
        public bool TryRemoveToTypeStore(WrapperType wrapperType, Action<UIDisplay> succeedBack = null)
        {
            bool result = false;
            if (ReadStore(wrapperType, out var readStore))
            {
                try
                {
                    if (readStore.standard != null && readStore.standard.Count > 0)
                    {
                        foreach (var item in readStore.standard)
                        {
                            succeedBack?.Invoke(item.Value);
                        }
                        readStore.standard.Clear();
                    }

                    InsideRemove();

                    void InsideRemove()
                    {
                        if (readStore.queue != null && readStore.queue.Count > 0)
                        {
                            UIDisplay insideUI = readStore.queue.Pop();
                            succeedBack?.Invoke(insideUI);
                            InsideRemove();
                        }
                    }
                    result = true;
                    UIFrame.Log($"尝试移除缓存器中类型的所有数据成功!", wrapperType, LogSystem.Store);
                }
                catch (Exception error)
                {
                    UIFrame.LogError($"尝试移除缓存器中{wrapperType}类型的所有数据失败! {error}", wrapperType , LogSystem.Store);
                    result = false;
                }
            }
            return result;
        }

        /// <summary>
        /// 尝试从缓存器中读取数据
        /// </summary>
        /// <param name="uiName"></param>
        /// <param name="UI"></param>
        /// <returns></returns>
        public bool TryReadToAllStore(string uiName, out UIDisplay UI)
        {
            if (allUIDict.TryGetValue(uiName, out UI))
            {
                UIFrame.Log($"尝试从总存储管理器中获取UI成功！", uiName, LogSystem.Store);
                return true;
            }
            else
            {
                UIFrame.LogError($"尝试从总存储管理器中获取UI失败,因为缓存器中没有这个值！", uiName, LogSystem.Store);
                return false;
            }
        }

        /// <summary>
        /// 尝试从缓存器获取当前队列并判断值在堆栈中是否存在
        /// </summary>
        /// <param name="UI"></param>
        /// <returns></returns>
        public bool TryContainsToQueueStore(UIDisplay UI)
        {
            bool result = false;
            if (ReadStore(UI.wrapperType, out var readStore))
            {
                try
                {
                    result = readStore.queue.Contains(UI);
                }
                catch (Exception error)
                {
                    UIFrame.LogError($"尝试从缓存器中获取队列数据失败! {error}", UI, LogSystem.Store);
                    return false;
                }
            }
            return result;
        }

        /// <summary>
        /// 获取所有正在显示的UI
        /// </summary>
        public List<UIDisplay> GetAllOnDisplay()
        {
            List<UIDisplay> onDisplayUis = new();
            foreach (var item in allUIDict)
            {
                if (item.Value.gameObject.activeSelf)
                    onDisplayUis.Add(item.Value);
            }
            return onDisplayUis;
        }
    }
}
